package com.shenqi.language.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shenqi.language.mapper.UsermarkMapper;
import com.shenqi.language.domain.Usermark;
import com.shenqi.language.service.IUsermarkService;

/**
 * 用户词条标记Service业务层处理
 * 
 * @author shenqi
 * @date 2025-08-05
 */
@Service
public class UsermarkServiceImpl implements IUsermarkService 
{
    @Autowired
    private UsermarkMapper usermarkMapper;

    /**
     * 查询用户词条标记
     * 
     * @param userId 用户词条标记主键
     * @return 用户词条标记
     */
    @Override
    public Usermark selectUsermarkByUserId(Long userId)
    {
        return usermarkMapper.selectUsermarkByUserId(userId);
    }

    /**
     * 查询用户词条标记列表
     * 
     * @param usermark 用户词条标记
     * @return 用户词条标记
     */
    @Override
    public List<Usermark> selectUsermarkList(Usermark usermark)
    {
        return usermarkMapper.selectUsermarkList(usermark);
    }

    /**
     * 新增用户词条标记
     * 
     * @param usermark 用户词条标记
     * @return 结果
     */
    @Override
    public int insertUsermark(Usermark usermark)
    {
        usermark.setCreateTime(DateUtils.getNowDate());
        return usermarkMapper.insertUsermark(usermark);
    }

    /**
     * 修改用户词条标记
     * 
     * @param usermark 用户词条标记
     * @return 结果
     */
    @Override
    public int updateUsermark(Usermark usermark)
    {
        usermark.setUpdateTime(DateUtils.getNowDate());
        return usermarkMapper.updateUsermark(usermark);
    }

    /**
     * 批量删除用户词条标记
     * 
     * @param userIds 需要删除的用户词条标记主键
     * @return 结果
     */
    @Override
    public int deleteUsermarkByUserIds(Long[] userIds)
    {
        return usermarkMapper.deleteUsermarkByUserIds(userIds);
    }

    /**
     * 删除用户词条标记信息
     * 
     * @param userId 用户词条标记主键
     * @return 结果
     */
    @Override
    public int deleteUsermarkByUserId(Long userId)
    {
        return usermarkMapper.deleteUsermarkByUserId(userId);
    }

    @Override
    public void mark(Usermark usermark) {
        if(usermark.getVocabularyStatus() == null) {
            usermarkMapper.deleteUsermark(usermark);
            return;
        }
        Usermark query = new Usermark();
        query.setUserId(usermark.getUserId());
        query.setVocabularyId(usermark.getVocabularyId());
        List<Usermark> marks = usermarkMapper.selectUsermarkList(query);
        if(marks.size() == 0) {
            usermarkMapper.insertUsermark(usermark);
        } else {
            usermarkMapper.updateUsermark(usermark);
        }
    }

}
